package base.innerclass;

/**
 * @Author Fizz Pu
 * @Date 2021/4/10 上午10:41
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */

// 通过内部类实现迭代器

public class ImplementsIteator {
    public static void main(String[] args) {
        Sequence sequence = new Sequence(10);
        for(int i = 0; i < 10; i++){
            sequence.add(i);
        }
        Selector selector = sequence.getIterator();
        while (selector.hasNext()){
            System.out.println(selector.next());
        }
    }
}

interface Selector{
    boolean hasNext();
    Object next();
}

class Sequence{
    private int next = 0;
    private Object[] arr;

    public Sequence(int size){
        arr = new Object[size];
    }

    public boolean add(Object ob){
        if(next >= arr.length){
            return  false;
        }

        arr[next++] = ob;
        return true;
    }

    private class SequenceSelector implements Selector{
        private int index;

        @Override
        public boolean hasNext() {
            if(index < arr.length){
                return true;
            }
            return false;
        }

        @Override
        public Object next() {
            return arr[index++];
        }
    }

    public  Selector getIterator(){
        return new SequenceSelector();
    }
}


